<template>
  <div>
    <el-dialog title="设置等级" :close-on-click-modal="false" :visible.sync="visible" width="30%">
      <el-form ref="form" :rules="rules" v-loading="loading" :inline="true" :model="pamars">
        <el-form-item label="等级" prop="level">
          <el-select v-model="pamars.level" clearable placeholder="请选择会员等级">
            <el-option v-for="(item,index) in levelList" :key="index" :label="item.outer_role_name" :value="item.level"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form>
        <el-form-item style="text-align: right;margin-top: 20px;">
          <el-button @click="submit()">
            确定
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import seviceApi from '@/api/serviceApi.js'
export default {
  props: ['data'],
  data () {
    return {
      rules: {
        level: [
          { required: true, message: '请选择等级', trigger: 'change' }
        ]
      },
      visible: false,
      pamars: {
        level: ''
      },
      levelList: [],
      loading: false
    }
  },
  created () {
    this.getShopConfig()
  },
  methods: {
    getShopConfig () {
      seviceApi.getShopConfig().then(res => {
        if (res && res.data.code == 200) {
          this.levelList = res.data.data
        }
        // else {
        //   this.$message.warning(res.data.msg)
        // }
      })
    },
    submit () {
      let that = this
      this.$confirm('是否重新设置等级?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.$refs['form'].validate((valid) => {
          if (valid) {
            that.loading = true
            seviceApi.updateVIPLevel({
              userId: that.data.id,
              level: that.pamars.level
            }).then(res => {
              that.loading = false
              if (res && res.data.code == 200) {
                that.$message.success('设置等级成功')
                that.$emit('success')
                that.visible = false
              }
            })
              .catch(err => {
                that.loading = false
              })
          }
        })
      })
    }
  }
}
</script>

<style>
</style>
